package com.zlx.mall.biz.attribution.controller;

import com.zlx.mall.basic.trace.MDCUtil;
import com.zlx.mall.basic.util.json.JacksonUtil;
import com.zlx.mall.basic.web.holder.RequestContextHolder;
import com.zlx.mall.basic.web.model.BaseRequest;
import com.zlx.mall.basic.web.model.BaseResponse;
import com.zlx.mall.biz.attribution.model.AppErrorLine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qingke
 **/
@Slf4j(topic = "appErrorLogger")
@RestController
public class AppErrorController {

    @RequestMapping(path = "/api/app/error/v1", method = {RequestMethod.POST})
    public BaseResponse saveAppError(BaseRequest baseRequest) {
        AppErrorLine line = new AppErrorLine();
        line.setRemoteAddress(baseRequest.getRemoteAddress());
        line.setTimestamp(System.currentTimeMillis());
        line.setTraceId(MDCUtil.getTraceId());
        line.setSystemParam(baseRequest.getSystem());
        line.setUserId(baseRequest.getUserId());
        line.setContent(RequestContextHolder.getContent());
        log.info("{}", JacksonUtil.toJsonString(line));
        return BaseResponse.success();
    }
}
